﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using hlavny.modul2;
using modul3;
using System.IO;

namespace hlavny
{
    public class CDBS
    {
        private static CSysDatabases sysDatabases = null;
        private static CSysTables sysTables = null;
        private static CSysColumns sysColumns = null;
        private static SysIndex sysIndex = null;
        private static SysConstr sysConstr = null;

        protected CDBS()
        {

        }

        /// <summary>
        /// Property for static sysDatabases
        /// </summary>
        public static CSysDatabases SysDatabases 
        {
            get
            {
                if (sysDatabases == null) 
                {
                    if (File.Exists(CConfig.SysDatabasesFileName))
                    {
                        CFunctions.LoadSysDatabases(ref sysDatabases);
                    }
                    else
                    {
                        sysDatabases = new CSysDatabases();
                    }
                }
                return sysDatabases;
            }
        }

        /// <summary>
        /// Property for static sysTables
        /// </summary>
        public static CSysTables SysTables
        {
            get
            {
                if (sysTables == null)
                {
                    if (File.Exists(CConfig.SysTablesFileName))
                    {
                        CFunctions.LoadSysTables(ref sysTables);
                    }
                    else
                    {
                        sysTables = new CSysTables();
                    }
                }
                return sysTables;
            }
        }

        /// <summary>
        /// Property for static sysColumns
        /// </summary>
        public static CSysColumns SysColumns
        {
            get
            {
                if (sysColumns == null)
                {
                    if (File.Exists(CConfig.SysColumnsFileName))
                    {
                        CFunctions.LoadSysColumns(ref sysColumns);
                    }
                    else
                    {
                        sysColumns = new CSysColumns();
                    }
                }
                return sysColumns;
            }
        }

        /// <summary>
        /// Property for static sysIndex
        /// </summary>
        public static SysIndex SysIndex
        {
            get
            {
                if (sysIndex == null)
                {
                    if (File.Exists(cConfigModule3.sSysIndexFileName))
                    {
                        FunctionsModule3.LoadSysIndex(ref sysIndex);
                    }
                    else
                    {
                        sysIndex = new SysIndex();
                    }
                }
                return sysIndex;
            }
        }

        /// <summary>
        /// Property for static sysConstr
        /// </summary>
        public static SysConstr SysConstr
        {
            get
            {
                if (sysConstr == null)
                {
                    if (File.Exists(cConfigModule3.sSysConstrFileName))
                    {
                        FunctionsModule3.LoadSysConstr(ref sysConstr);
                    }
                    else
                    {
                        sysConstr = new SysConstr();
                    }
                }
                return sysConstr;
            }
        }

        public static bool DropColumn(CAttribute attribute)
        {
            SysIndex.dropAllFromColumn(attribute.Id);
            SysConstr.dropAllFromColumn(attribute.Id);
            SysTables.DrobAtr(attribute);
            SysColumns.DropAtr(attribute);
            return true;
        }


        public static bool DropTable(CTable table)
        {
            table.ReloadColumns(SysColumns);
            SysTables.CurrentTable = table;
            foreach (CAttribute attribute in table.ColumnsRefs)
            {
                DropColumn(attribute);
            }
            SysDatabases.DropTable(table);
            SysTables.DropTab(table);
            return true;
        }

        public static bool DropDatabase(CDatabase database)
        {
            database.ReloadTables(SysTables);
            SysDatabases.CurrentDatabase = database;
            foreach (CTable table in database.TablesRefs)
            {
                DropTable(table);
            }
            SysDatabases.DropDatabase(database);
            return true;
        }
    }
}
